package com.wave.gate.controller;

import com.wave.gate.enums.ConveyerEnum;
import com.wave.gate.util.GuavaCache;
import com.wave.gate.util.SocketClientUtil;
import com.wave.gate.vo.GatePassSetVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.Socket;

/**
 * @author linhui
 * @date 2023/5/26 18:23
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/testConveyer")
public class TestConveyerController {
    /**
     * 测试传送带type=7入口，type=8出口
     */
    @GetMapping("/test/{type}")
    public void test(@PathVariable String type) throws IOException {
        Socket conveyer = null;
        try {
            //左进右出0->1->2/3   右进左出4->5->2/3
            //0->1/7    4->5/8

            GatePassSetVo gatePassSetVo = GuavaCache.PASSSET_CACHE.getIfPresent("passsetipInfo");

             conveyer = new Socket(gatePassSetVo.getPclIp(), 502);
            String str = "";
            if("1".equals(type)){
                // 0 1 2
                 str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M0.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M1.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M2.getCode());
            }
            if("2".equals(type)){
                // 0 1 3
                 str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M0.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M1.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M3.getCode());
            }

            if("3".equals(type)){
                // 4 5 2
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M4.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M5.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M2.getCode());
    //            Thread.sleep(300);
            }

            if("4".equals(type)){
                // 4 5 3
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M4.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M5.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M3.getCode());
    //            Thread.sleep(300);
            }

            if("5".equals(type)){
                // 0  7
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M0.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M7.getCode());
    //            Thread.sleep(300);
            }

            if("6".equals(type)){
                // 4  8
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M4.getCode());
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M8.getCode());
    //            Thread.sleep(300);
            }

            if("7".equals(type)){
                // M9左侧强制退出
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M9.getCode());
            }

            if("8".equals(type)){
                // M10右侧强制退出
    //            Thread.sleep(300);
                str = SocketClientUtil.sendData(conveyer, ConveyerEnum.CONVEYER_COMMAND_M10.getCode());
            }
        } catch (IOException e) {
            log.error("test conveyer error: [{}]",e.getMessage(),e);
        }finally {
            if(conveyer != null){
                conveyer.close();
            }
        }


    }
}
